/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TestGui.java
 *
 * Created on Nov 1, 2010, 8:17:30 PM
 */
package uploadphotobucket;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author banhbaochay
 */
public class TestGui extends javax.swing.JFrame {

    /** Creates new form TestGui */
    public TestGui() {
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equalsIgnoreCase(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
        }
        Vector cols = new Vector();
        cols.add("col 1");
        cols.add("col 2");
        cols.add("col 3");
        cols.add("col 4");
        Vector data = new Vector();
        for (int i = 0; i < 5; i++) {
            Vector tmp = new Vector();
            tmp.add("row " + i + " col 1");
            tmp.add("row " + i + " col 2");
            tmp.add("row " + i + " col 3");
            data.add(tmp);
        }
        m = new DefaultTableModel(data, cols);
        initComponents();

        /* set same size buttons */
        try {
            Class c = Class.forName("uploadphotobucket.TestGui");
//            Field[] fields = c.getDeclaredFields();
//            for (Field f : fields) {
//                if (f.getType().getName().equals("javax.swing.JButton")) {
//                    try {
//                        System.out.println(f.get(c));
//                    } catch (IllegalArgumentException ex) {
//                        Logger.getLogger(TestGui.class.getName()).log(Level.SEVERE, null, ex);
//                    } catch (IllegalAccessException ex) {
//                        Logger.getLogger(TestGui.class.getName()).log(Level.SEVERE, null, ex);
//                    }
//                }
//            }
        } catch (Exception ex) {
            Logger.getLogger(TestGui.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(600, 400));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton1.setText("Cancel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 250, -1, -1));

        jTable1.setModel(m);
        jScrollPane1.setViewportView(jTable1);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(44, 19, -1, 305));

        jButton2.setText("jButton2");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 40, -1, -1));

        jButton3.setText("b3");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 110, -1, -1));

        jLabel1.setText("jLabel1");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(588, 176, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (worker == null) {
            System.out.println("no");
        } else {
            if (!worker.isDone()) {
                worker.cancel(true);
                worker = null;
                m.setValueAt("Cancelled", 0, 2);
                JOptionPane.showMessageDialog(null, "Cancel");
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        worker = new AnswerWorker();
        worker.execute();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        jLabel1.setText("Le Anh Dung");
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TestGui().setVisible(true);
            }
        });
    }

    class AnswerWorker extends SwingWorker<Void, Void> {

        @Override
        protected Void doInBackground() throws Exception {
            while (!isCancelled()) {
                setCursor(new Cursor(Cursor.WAIT_CURSOR));
                int val;
                for (int i = 0; i < m.getRowCount(); i++) {
                    val = 0;
                    while (val < 100) {
                        Thread.sleep(1000);
                        val += new Random().nextInt(20);
                        if (val > 100) {
                            m.setValueAt("Done", i, 1);
                        } else {
                            m.setValueAt(val, i, 1);
                        }
                    }
                }
                JOptionPane.showMessageDialog(null, "Done");
            }
            return null;
        }

        @Override
        protected void done() {
            setCursor(null);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
    DefaultTableModel m;
    private AnswerWorker worker;
    private int abc = 3;
}
